package com.cungmua.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.model.DrawerChild;
import com.cungmua.app.model.DrawerParent;

import java.util.HashMap;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/17/14.
 */
public class BaseExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<DrawerParent> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<Integer, List<DrawerChild>> _listDataChild;

    public BaseExpandableListViewAdapter(Context context,
                                         List<DrawerParent> listDataHeader,
                                         HashMap<Integer, List<DrawerChild>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public DrawerChild getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(groupPosition).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = getChild(groupPosition, childPosition).getTextCenter();

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.drawer_list_subitem,
                    null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.tvSubItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (this._listDataChild.get(groupPosition) == null)
            return 0;
        return this._listDataChild.get(groupPosition).size();
    }

    @Override
    public DrawerParent getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        DrawerParent header = getGroup(groupPosition);
        // if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (header.isHeader()) {
            convertView = infalInflater.inflate(R.layout.drawer_header_item,
                    null);
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.tvTextHeader);
            lblListHeader.setText(header.getTextCenter());
        } else {
            if (groupPosition == 141) {
                // cart item
                convertView = infalInflater.inflate(
                        R.layout.drawer_list_cart_item, null);
                ImageView imgIconLeft = (ImageView) convertView
                        .findViewById(R.id.ivIconLeft);
                if (header.getIconLeft() != 0) {
                    imgIconLeft.setImageResource(header.getIconLeft());
                }
                TextView lblListTitle = (TextView) convertView
                        .findViewById(R.id.tvTextCenter);
                TextView lblListTextRight = (TextView) convertView
                        .findViewById(R.id.tvTextRight);
                lblListTitle.setText(header.getTextCenter());
                lblListTextRight.setText(header.getTextRight());
            } else {
                convertView = infalInflater.inflate(R.layout.drawer_list_item,
                        null);
                ImageView imgIconLeft = (ImageView) convertView
                        .findViewById(R.id.ivIconLeft);
                ImageView imgGroupIndicator = (ImageView) convertView
                        .findViewById(R.id.ivGroupIndicator);
                if (header.isExpandable()) {
                    if (isExpanded) {
                        imgGroupIndicator
                                .setImageResource(R.drawable.ic_up_arrow);
                    } else {
                        imgGroupIndicator
                                .setImageResource(R.drawable.ic_down_arrow);
                    }
                }
                if (header.getIconLeft() != 0) {
                    imgIconLeft.setImageResource(header.getIconLeft());
                }else{
                    imgIconLeft.setVisibility(View.GONE);
                }
                TextView lblListTitle = (TextView) convertView
                        .findViewById(R.id.tvTextCenter);
                lblListTitle.setText(header.getTextCenter());
            }

        }
        // }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
